package com.yoso.newx;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.util.RandomUtil;
import com.yoso.domain.User;
import org.apache.commons.lang3.function.TriFunction;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author shizejin
 * @date 2022/1/27
 * @description:
 */
public class MyConsumer {
    /**
     *
     */

    public Consumer<User> buildUser() {
        return (user) -> {
            user.setName("consumer");
            user.setAge(28);
        };
    }

    public int add(int a, int b) {
        return a + b;
    }

    public Consumer<User> setUserAddr() {
        return (user) -> user.setAddr("北京");
    }

    /**
     *
     */
    @Test
    public void test() {
        User user = new User();
        LineHandler lineHandler = System.out::println;
        lineHandler.handle("mmmmmm");
        TriFunction<MyConsumer, Integer, Integer, Integer> add = MyConsumer::add;
        Integer apply = add.apply(new MyConsumer(), 1, 3);
        System.out.println("apply = " + apply);
        buildUser().andThen(setUserAddr()).accept(user);
        System.out.println(user);
    }


    /**
     * 2
     */
    @Test
    public void test2() {
        List<String> list = CollUtil.toList("1", "2", "3");
        Function<RandomUtil, String> key = r -> RandomUtil.randomString(3);
        list.parallelStream().collect(Collectors.toMap(
                e -> e + "x", Function.identity(), (v1, v2) -> v2
        ));
        String apply = key.apply(new RandomUtil());
        System.out.println(apply);
        Optional<String> optional = Optional.ofNullable(apply);

    }
}
